package com.cssy.xxkj.logic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.dao.LogicPurchaseDAO;
import com.cssy.xxkj.logic.dao.LogicPurchaseItemDAO;
import com.cssy.xxkj.logic.entity.LogicPurchaseDO;
import com.cssy.xxkj.logic.entity.LogicPurchaseItemDO;
import com.cssy.xxkj.logic.service.ILogicPurchaseService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicPurchaseService")
public class LogicPurchaseServiceImpl implements ILogicPurchaseService{

	@Resource(name = "logicPurchaseDAO")
	private LogicPurchaseDAO logicPurchaseDAO;
	
	@Resource(name = "logicPurchaseItemDAO")
	private LogicPurchaseItemDAO logicPurchaseItemDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicPurchaseDO> list = logicPurchaseDAO.find(scs);
		rs.setRecordCount(logicPurchaseDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return logicPurchaseDAO.deleteLogicPurchaseDOByPrimaryKey(id);
	}

	@Override
	public Integer update(LogicPurchaseDO pchdo, User user) throws SyBaseException {
		pchdo.setBizModifyName(user.getName());
		pchdo.setBizModifyDate(new Date());
		pchdo.setBizVersionMark(pchdo.getBizVersion());
		pchdo.setBizVersion(pchdo.getBizVersion()+Integer.valueOf(1));
		return logicPurchaseDAO.updateLogicPurchaseDO(pchdo);
	}

	@Override
	public LogicPurchaseDO save(LogicPurchaseDO pudo, User user)
			throws SyBaseException {
		pudo.setBizCreatorName(user.getName());
		pudo.setBizCreatorDate(new Date());
		pudo.setBizVersion(Integer.valueOf(1));
		logicPurchaseDAO.insertLogicPurchaseDO(pudo);
		return pudo;
	}

	@Override
	public LogicPurchaseDO findListById(String id) throws SyBaseException {
		return logicPurchaseDAO.findLogicPurchaseDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return logicPurchaseDAO.deleteBizRemoved(id);
	}

	/**
	 * 采购订单
	 */
	@Override
	public void purchaseSave(HttpServletRequest request,
			LogicPurchaseDO curAP) throws DataAccessException {
		try{
			String purchaseId = request.getParameter("id");
			String itemdata = request.getParameter("itemdata");//物品数据
			String deldata = request.getParameter("itemdelete");//物品数据
			String zd019OrderDate = request.getParameter("zd019OrderDate");//订购日期
			String zd021AcceptDate = request.getParameter("zd021AcceptDate");//收货日期
			String zdFlag = request.getParameter("flag");//标识
			//订单日期
			if(StringUtils.isBlank(zd019OrderDate)){
				curAP.setZd019OrderDate(null);
			}else{
				curAP.setZd019OrderDate(DateUtils.stringToDate(zd019OrderDate));
			}
			//收货日期
			if(StringUtils.isBlank(zd021AcceptDate)){
				curAP.setZd021AcceptDate(null);
			}else{
				curAP.setZd021AcceptDate(DateUtils.stringToDate(zd021AcceptDate));
			}
			
			List<LogicPurchaseItemDO> itemAddList = JSONArray.parseArray(itemdata,LogicPurchaseItemDO.class);//新增
			if(BillStatusUtil.PHS_BILL_STATUS_FOUR.equals(zdFlag)){
				//驳回
				LogicPurchaseDO purchaseDO = logicPurchaseDAO.findLogicPurchaseDOByPrimaryKey(purchaseId);
				if(purchaseDO==null){
					throw new SyBaseException("您修改的行数据发生改变，请重新点开查看");
				}
				purchaseDO.setZd011Status(BillStatusUtil.PHS_BILL_STATUS_FOUR);
				this.update(purchaseDO, SessionUtil.getUser(request));
			}else{
				String keyId = "";
				//采购订单的入库状态默认为未入库
				curAP.setZd011WhStatus(BillStatusUtil.PCSWH_BILL_STATUS_ZERO);
				if(curAP==null || StringUtils.isBlank(curAP.getId())){
					//新增
					keyId = IDCodeTypeUtil.getId();
					curAP.setId(keyId);
					curAP.setZd020Code(syCodeService.getCode(BillTypeConstant.PURCHASE,BillTypeConstant.PURCHASE,SyCode.DATE_YYMM,5));
					curAP.setBizVersion(Integer.valueOf(1));
					curAP.setZd011Status(zdFlag);
					//创建者人/时间
					curAP.setBizCreatorDate(new Date());
					curAP.setBizCreatorName(SessionUtil.getUser(request).getRealName());
					this.save(curAP, SessionUtil.getUser(request));
				}else{
					//修改
					keyId=curAP.getId();
					curAP.setZd011Status(zdFlag);
					Integer result = this.update(curAP, SessionUtil.getUser(request));
					if (Integer.valueOf(1).equals(result)) {
						if (StringUtils.isNotBlank(deldata)) {
							List<LogicPurchaseItemDO> itemDelList = JSONArray
									.parseArray(deldata,LogicPurchaseItemDO.class);// 删除
							if (itemDelList == null) {
								itemDelList = new ArrayList<LogicPurchaseItemDO>();
							}
							for (LogicPurchaseItemDO pusItemDO : itemDelList) {
								if (pusItemDO == null|| StringUtils.isBlank(pusItemDO.getId())) {
									continue;
								}
								logicPurchaseItemDAO.deleteLogicPurchaseItemDOByPrimaryKey(pusItemDO.getId());
							}
						}
					} else {
						throw new SyBaseException("您修改的行数据发生改变，请重新点开查看");
					}
				}
				//采购订单新增Item
				if(itemAddList!=null && !itemAddList.isEmpty() && itemAddList.size()>0){
					for(LogicPurchaseItemDO purchaseItemDO : itemAddList){
						if(purchaseItemDO==null){
							continue;
						}
						if(StringUtils.isBlank(purchaseItemDO.getId())){
							purchaseItemDO.setId(IDCodeTypeUtil.getId());
							purchaseItemDO.setZd001PurchaseId(keyId);
							logicPurchaseItemDAO.insertLogicPurchaseItemDO(purchaseItemDO);
						}else{
							logicPurchaseItemDAO.updateLogicPurchaseItemDO(purchaseItemDO);
						}
					}
				}
			}
		}catch(Exception e){
			throw new SyBaseException("");
		}
	}

}
